package gui.win.initializer;

import core.para.Para;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class Buttons extends JPanel implements ActionListener {

    private JButton close, process;
    private Initializer init;

    public Buttons(Initializer init) {
        super(new BorderLayout());
        this.init = init;
        this.add(getButton(close = new JButton("CANCEL"), 200, 22, new Color(102, 102, 102), new Color(178, 178, 178), this, "CANCEL"), BorderLayout.WEST);
        this.add(getButton(process = new JButton("LAUNCH"), 200, 22, new Color(255, 0, 0), new Color(178, 178, 178), this, "LAUNCH"), BorderLayout.EAST);
    }

    public void actionPerformed(ActionEvent evt) {
        Object source = evt.getSource();
        if (source == close) {
            init.dispose();
        } else if (source == process) {
            init.setCommand(this.getClass());
            
            Runtime rt = Runtime.getRuntime();
            
            //System.out.println(Para.LAUNCH_COMMAND);
            try {
                Process p = rt.exec(Para.LAUNCH_COMMAND);
                p.getInputStream().close();
                p.getOutputStream().close();
                p.getErrorStream().close();
            } catch (Exception e) {e.printStackTrace();}
            init.dispose();
            System.exit(0);
        }
    }

    public void closeFrame() {
        init.dispose();
        System.exit(0);
    }

    public static JPanel getButton(JButton st, int width, int height, Color fCol, Color bCol, ActionListener al, String tooltip) {
        st.setForeground(fCol);
        st.setBackground(bCol);
        st.setPreferredSize(new Dimension(width, height));
        st.addActionListener(al);
        st.setToolTipText(tooltip);
        JPanel p = new JPanel(new FlowLayout());
        p.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        p.setOpaque(false);
        p.add(st);
        return p;
    }
}
